<?php
	/**
	* modified by jszhang, change 'cityList' -> 'citylist'
	*
	*/
	include_once ( "AbstractDAO.php" );
	class CityDAO extends AbstractDAO
	{
		//Get all city list
		function GetCityList()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "CityList", CNS_INDEX );
			if( $re !== false )
				return $re;
				
			$result = $this->db->GetPlan( "select id, name from citylist where parentid>0" );
			
			$cityList = array();
			foreach ( $result as $item )
			{
				$cityList[$item['name']] = $item['id'];
			}
			
			$cacheManager->Set( "CityList", $cityList, CNS_INDEX );
			return $cityList;
		}
		
		//Get all province list
		function GetProvinceList()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "ProvinceList", CNS_INDEX );
			if( $re !== false )
				return $re;
				
			$result = $this->db->GetPlan( "select id, name from citylist where parentid=0" );
			
			$provinceList = array();
			foreach ( $result as $item )
			{
				$provinceList[$item['name']] = $item['id'];
			}
			$cacheManager->Set( "ProvinceList", $cityList, CNS_INDEX );
			return $provinceList;
		}
				
		/**
		 * Get Location id from location map
		 *
		 * @global map $cityMap
		 * @global map $provinceMap
		 * @param string $name
		 * @return int
		 */
		function GetCityID( $name )
		{
		    $list=$this->GetCityList();
		    foreach( $list as $key=>$value )
		    {
		        if( strpos($name, $key ) === false )
		        {
		            continue;
		        }
		        else
		        {
		            return $value;
		        }
		    }
			$list=$this->GetProvinceList();
		    foreach( $list as $key=>$value )
		   	{
		        if( strpos( $name, $key ) === false )
		        {
		            continue;
		        }
		        else
		        {
		            return $value;
		        }
		    }
		    return 0; //找不到返回0
		}
		
		function GetProvinceIDByCityID($cityID)
		{
			$sql="select parentid from citylist where id=:cityID";
			$psql = $this->db->CreatePreparedStatement($sql);
			$psql->AddParameter( 'cityID', SQLTypes::INTEGER, $cityID );
			return $this->db->GetValue($psql);
		}

		//Map from location id to location name
		function GetCityName($locationid)
		{
			if($locationid=='0')
			{
			    return '';//取不到显示为空
			}
			$sql="select name, parentid from citylist where id=$locationid";
			$result=$this->db->GetRow($sql);
			if($result==false)
				return '';
			else
			{
				if($result['parentid']=='0')
					return $result['name'];
				else
				{
					$city=$result['name'];
					$proID=$result['parentid'];
					$sql="select name, parentid from citylist where id=$proID";
					$result=$this->db->GetRow($sql);
					if($result==false)
						return $city;
					else
						return $result['name'].' '.$city;
				}
			}
	    }		
		
		//Map from location id to location name
		function GetCityNameSimple($locationid)
		{
			if($locationid=='0')
			{
			    return '';//取不到显示为空
			}
			$sql="select name, parentid from citylist where id=$locationid";
			$result=$this->db->GetRow($sql);
			if($result==false)
				return '';
			else
			{
				return $result['name'];
			}
	    }
		
	    
	    function GetHotCity()
	    {
	    	$sql="select id, name from citylist where parentid=0";
	    	return $this->db->GetPlan($sql);
	    }
	} 

?>
